<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/customer.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <div id="htabs" class="htabs"><a href="#tab-general">Thông Tin</a> <a href="#tab-customer-info">Liên Hệ</a>
        <?php if ($customer_id) { ?>
        <a href="#tab-history">Thu Tiền Lần 2</a>
        <a href="#tab-transaction">Lịch Sử Nhận Tiền</a>
        <?php } ?></div>
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <div id="tab-general">
            <table class="form">
              <tr>
                <td><span class="required">*</span>Mã Giới Thiệu: </td>
                <td><?php if ($customer_id) { ?><input readonly="readonly" type="text" name="customer" value="<?php echo $customer; ?>" />
				<?php } else {?> <input type="text" name="customer" value="<?php echo $customer; ?>" />
				<?php }?>
				  <input type="hidden" name="magioithieu" value="<?php echo $magioithieu; ?>" />	
                  <?php if ($error_customer) { ?>
					<span class="error"><?php echo $error_customer; ?></span>
                  <?php } ?></td>
              </tr> <tr>
                <td><span class="required">*</span>Mã Chỉ Định: </td>
                <td><?php if ($customer_id) { ?><input readonly="readonly" type="text" name="customer_chidinh" value="<?php echo $customer_chidinh; ?>" />
				<?php } else {?> <input type="text" name="customer_chidinh" value="<?php echo $customer_chidinh; ?>" />
				<?php }?>
				  <input type="hidden" name="machidinh" value="<?php echo $machidinh; ?>" /></td>
              </tr> <tr>
                <td><span class="required">*</span>Mã Đăng Nhập: </td>
                <td><?php if ($customer_id) { ?><input readonly="readonly" type="text" name="madn" value="<?php echo $madn; ?>" />
				<?php } else {?> <input type="text" name="madn" value="<?php echo $madn; ?>" />
				<?php }?>
                  <?php if ($error_madn) { ?>
                  <span class="error"><?php echo $error_madn; ?></span>
                  <?php } ?></td>
              </tr>
              <tr>
                <td><span class="required">*</span> Tiền Đăng Ký: </td>
                <td><?php if ($customer_id) { ?><input readonly="readonly" type="text" name="price" value="<?php echo $price; ?>" />
				<?php } else {?> <input type="number" name="price" value="<?php echo $price; ?>" /><?php }?>
                  <?php if ($error_price) { ?>
                  <span class="error"><?php echo $error_price; ?></span>
                  <?php  } ?></td>
              </tr>
              <tr>
                <td><?php echo $entry_password; ?></td>
                <td><input type="password" name="password" value="<?php echo $password; ?>"  />
                  <?php if ($error_password) { ?>
                  <span class="error"><?php echo $error_password; ?></span>
                  <?php  } ?></td>
              </tr>
              <tr>
                <td><?php echo $entry_confirm; ?></td>
                <td><input type="password" name="confirm" value="<?php echo $confirm; ?>" />
                  <?php if ($error_confirm) { ?>
                  <span class="error"><?php echo $error_confirm; ?></span>
                  <?php  } ?></td>
              </tr>
              <tr>
				<td>Trạng Thái:</td>
                <td>
					<?php if ($status) { ?>
						Thành viên đã được kích hoạt
					<?php } else { ?>
						Thành viên chưa được kích hoạt
					<?php } ?>
				</td>
              </tr>
            </table>
        
        </div>
		<div id="tab-customer-info">
            <table class="form">
              <tr>
                <td><span class="required">*</span>Tên: </td>
                <td><input type="text" name="name" value="<?php echo $name; ?>" />
                  <?php if ($error_name) { ?>
                  <span class="error"><?php echo $error_name; ?></span>
                  <?php } ?></td>
              </tr>
              <tr>
                <td><?php echo $entry_email; ?></td>
                <td><input type="text" name="email" value="<?php echo $email; ?>"  />
                  <?php if ($error_email) { ?>
                  <span class="error"><?php echo $error_email; ?></span>
                  <?php  } ?></td>
              </tr>
              <tr>
                <td>Điện Thoại: </td>
                <td><input type="text" name="telephone" value="<?php echo $telephone; ?>"  /></td>
              </tr>
			  <tr>
                <td>Ngày Sinh: </td>
                <td><input type="text" name="born" value="<?php echo $born; ?>"  /></td>
              </tr>
			  <tr>
                <td>Địa Chỉ: </td>
                <td><input type="text" name="address" value="<?php echo $address; ?>"  /></td>
              </tr>
			  <tr>
                <td>Số tài khoản: </td>
                <td><input type="text" name="bank" value="<?php echo $bank; ?>"  /></td>
              </tr>
            </table>
          </div>
		  </form> 
		   <?php if ($customer_id) { ?>
        <div id="tab-history">
          <div id="history">
				<table class="list">
					<thead>
						<tr>
							<td>Lần Đóng Đóng</td>
							<td>Tiền Đóng</td>
							<td>Ngày Đóng</td>
						</tr>
					</thead>
					<?php if($price){?>
						<tr>
						  <td>Đóng lần 1</td>
						  <td><span><?php echo $price;?></span></td>
						  <td><span><?php echo $date_added;?></span></td>
						</tr>
					<?php }?>
					<?php if($history){?>
						<tr>
						  <td>Đóng Lần 2</td>
						  <td><span><?php echo $history['price'];?></span></td>
					      <td><span><?php echo $history['date_added'];?></span></td>
						</tr>	
					<?php }?>		
			  </table>
			  <?php if(!$status){?>
				 <form action="<?php echo $action_update; ?>" method="post" enctype="multipart/form-data" id="form_update">
					 <table class="list" style="widht:50%;">
						<tr>
						  <td>Số Tiền Đóng: </td>
						  <td><input type="text" readonly="readonly" name="price_update" value="<?php echo $config_price - $price;?>" /></td>
						  <td><a onclick="addPrice();" class="button">Đóng</a></td>
						</tr>
					</table>
					<p><span class="help">Ghi Chú: Thành viên đã đóng <?php echo $price;?>, số tiền thu lần 2 là : <?php echo $config_price - $price;?></span></p>
				</form>	
			  <?php }?>
		  </div>
        </div>
        <div id="tab-transaction">
		<p>Tổng tiền: <b><?php echo $total;?></b></p>
			<table class="list">
					<thead>
						<tr>
							<td>Lần Nhận</td>
							<td>Tiền</td>
							<td>Ngày Nhận</td>
							<td>Ghi Chú</td>
						</tr>
					</thead>
					<?php if($transaction){?>
						<?php foreach($transaction as $result){?>
							<tr>
							  <td><?php echo $i = 1; $i++;?></td>
							  <td><span><?php echo $result['price'];?></span></td>
							  <td><span><?php echo $result['date_added'];?></span></td>
							  <td><span><?php echo $result['comment'];?></span></td>
							</tr>
						<?php }?>	
					<?php }?>		
			  </table>
        </div>
        <?php } ?>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
//Only input number and anphabet for madn
$('input[name=\'madn\']').keypress(function (e) {
    var regex = new RegExp("^[A-Z0-9,\b]+$");
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    }

    e.preventDefault();
    return false;
});
function addPrice() {
  if($('input[name=\'price_update\']').attr('value') == '' || $('input[name=\'price_update\']').attr('value') == 0){
	$('input[name=\'price_update\']').css('background-color','red');
  } else {
	$('#form_update').submit();
  }
}

$('input[name=\'customer\']').autocomplete({
	delay: 500,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=sale/customer/autocompleteCustomer&token=<?php echo $token; ?>&type=1&filter_customer=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.madn,
						value: item.customer_id
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'customer\']').attr('value', ui.item.label);
		$('input[name=\'customer_chidinh\']').attr('value', ui.item.label);
		$('input[name=\'magioithieu\']').attr('value', ui.item.value);
		$('input[name=\'machidinh\']').attr('value', ui.item.value);
	
		return false;
	},
	focus: function(event, ui) {
      return false;
   }
});

$('input[name=\'customer_chidinh\']').autocomplete({
	delay: 500,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=sale/customer/autocompleteCustomer&token=<?php echo $token; ?>&type=1&filter_customer=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.madn,
						value: item.customer_id
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'customer_chidinh\']').attr('value', ui.item.label);
		$('input[name=\'machidinh\']').attr('value', ui.item.value);
	
		return false;
	},
	focus: function(event, ui) {
      return false;
   }
});

//--></script> 
<script type="text/javascript"><!--
$('.htabs a').tabs();
$('.vtabs a').tabs();
//--></script> 
<?php echo $footer; ?>